<template>
  <el-card style="margin-bottom: 5px">
    <el-input clearable @clear="load" style="width: 260px; margin-right: 5px;" v-model="data.name" placeholder="请输入用户名查询" :prefix-icon="Search"></el-input>
    <el-button type="primary" @click="load">查 询</el-button>
    <el-button type="info" @click="reset">重 置</el-button>
  </el-card>
  <el-card style="margin-bottom: 5px">
    <el-table :data="data.tableData" style="width: 100%" :header-cell-style="{ color:'#333',backgroundColor:'#dde8ff'}">
      <el-table-column label="头像" width="100">
        <template #default="scope">
          <el-image :preview-src-list="[scope.row.avatar]" :preview-teleported="true" v-if="scope.row.avatar" :src="scope.row.avatar" alt="" style="width: 50px; height: 50px; display: block; border-radius: 5px"/>
          <el-image :preview-src-list="[scope.row.avatar]" :preview-teleported="true" v-else src="https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png" alt="" style="width: 50px; height: 50px; display: block; border-radius: 5px"/>
        </template>
      </el-table-column>
      <el-table-column width="100" prop="name" label="用户名" />
      <el-table-column width="100" prop="username" label="账号名" />
      <el-table-column width="300" prop="phone" label="联系电话" />
      <el-table-column width="300" prop="email" label="邮箱" />
      <el-table-column prop="time" label="创建时间" />
    </el-table>
  </el-card>
  <el-card style="margin-bottom: 5px">
    <el-pagination
        v-model:current-page="data.pageNum"
        v-model:page-size="data.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5, 10, 15]"
        :total="data.total"
        @current-change="load"
        @size-change="load"
    />
  </el-card>


  <!-- 使用子组件 UserDialog -->
  <Dialog
      v-model="data.formVisible"
      :form="data.form"
      @save="load"
  />
</template>

<script setup>
import {Search} from "@element-plus/icons-vue";
import {reactive} from "vue";
import request from "@/utils/request";
import Dialog from "./Dialog.vue";

const data = reactive({
  user: JSON.parse(localStorage.getItem('code_user') || '{}'),
  tableData: [],
  formVisible: false,
  pageNum: 1,
  pageSize: 5,
  total: 0,
  name: '',
  form: {}
});

// 查询数据
const load = () => {
  request.get('/user/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name
    }
  }).then(res => {
    data.tableData = res.data.list;
    data.total = res.data.total;
  }).catch(err => {
    ElMessage.error(err);
  });
};

load();

// 重置按钮
const reset = () => {
  data.name = '';
  load();
};
</script>